package com.szl.sbssh.controller;

import com.szl.sbssh.core.ServiceException;
import com.szl.sbssh.pageModel.base.Grid;
import com.szl.sbssh.pageModel.base.Json;
import com.szl.sbssh.pageModel.base.PageFilter;
import com.szl.sbssh.pageModel.sys.User;
import com.szl.sbssh.service.UserService;
import com.szl.sbssh.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * Author: songzhonglin
 * Date: 2018/4/17
 * Time: 9:56
 * Description:
 **/
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/manager")
    public String manager() {
        return "admin/userList";
    }

    @RequestMapping("/dataGrid")
    @ResponseBody
    public Grid dataGrid(User user, PageFilter ph) {
        Grid grid = new Grid();
        grid.setRows(userService.dataGrid(user,ph));
        grid.setTotal(userService.count(user,ph));
        return grid;
    }

    @RequestMapping("/addPage")
    public String addPage() {
        return "/admin/userAdd";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Json add(User user) {
        Json j = new Json();
        User u = userService.getByLoginName(user);
        if (u != null) {
            j.setMsg("用户名已存在!");
        } else {
            try {
                userService.add(user);
                j.setSuccess(true);
                j.setMsg("添加成功！");
            } catch (Exception e) {
                j.setMsg(e.getMessage());
            }

        }
        return j;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Json delete(String id) {
        Json j = new Json();
        try {
            String ids[] = id.split(",");
            for (String dd : ids) {
                userService.delete(Long.valueOf(dd));
            }
            j.setMsg("删除成功！");
            j.setSuccess(true);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request, Long id) {
        User u = userService.get(id);
        request.setAttribute("user", u);
        return "/admin/userEdit";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Json edit(User user) {
        Json j = new Json();
        try {
            userService.edit(user);
            j.setSuccess(true);
            j.setMsg("编辑成功！");
        } catch (ServiceException e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }

    @RequestMapping("/export")
    @ResponseBody
    public Json exportExcel(User user, PageFilter ph, HttpServletRequest request, HttpServletResponse response, String ids) {
        Json j = new Json();
        List<User> userList = null;
        if (StringUtil.isNotEmpty(ids)) {// 不为空是true
            userList = userService.exportExcelBySelect(ids);
        } else {
            userList =  userService.dataGrid(user, ph);
        }
        String fileName = UUID.randomUUID() + "";
        String titleName = "用户信息导出";
        LinkedList<Map<String, String>> list = createExcelRecord(userList);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        try {
//            ExcelExportUtil.generateExcel(list, titleName).write(os);
//            ExcelExportUtil.createStream(response, fileName, os);
//            j.setMsg("导出excel成功！");
//            j.setSuccess(true);
//        } catch (IOException e) {
//            j.setMsg(e.getMessage());
//            e.printStackTrace();
//        }
        return j;
    }

    // 导出设置值
    private LinkedList<Map<String, String>> createExcelRecord(List<User> userList) {
        LinkedList<Map<String, String>> listmap = new LinkedList<Map<String, String>>();
        User user = null;
        for (int j = 0; j < userList.size(); j++) {
            user = userList.get(j);
            LinkedHashMap<String, String> mapValue = new LinkedHashMap<String, String>();
            mapValue.put("序号", user.getId().toString());
            mapValue.put("登录名", user.getLoginname());
            mapValue.put("用户名", user.getName());
            mapValue.put("所属部门", user.getOrganizationName());
            //mapValue.put("性别", sysDictionaryServiceI.getNameByCode(user.getSex()));
            mapValue.put("电话", user.getPhone());
           // mapValue.put("用户类型", sysDictionaryServiceI.getNameByCode(user.getUsertype()));
            mapValue.put("是否默认", user.getIsdefault() == 0 ? "默认" : "否");
            mapValue.put("状态", user.getState() == 0 ? "正常" : "停用");
            mapValue.put("创建时间 ", user.getCreatedatetime().toString());
            listmap.add(mapValue);
        }
        return listmap;
    }
}
